import { Inject, Injectable, Optional } from "@angular/core";
import { castArray } from "lodash-es";
import { MonkFormDesign, MonkFormDesignConfig, MONK_FORMDEISGN_CONFIG } from "../monk-form-design.interface";

@Injectable({
  providedIn: "root"
})
export class PreviewFormDesignService {
  constructor(
    @Optional() @Inject(MONK_FORMDEISGN_CONFIG) public formDesignConfig: MonkFormDesignConfig,
  ) {
  }
  public getFlatList({list, parent, needParent}: {
    list: Array<MonkFormDesign.AllType>, parent?: any, needParent?:boolean
  }) {
    const result = []
    const recursive = (list, parent) => {
      list?.forEach(data => {
        if (needParent) {
          data.__parent = parent
        }
        if (['tabs', 'grid', 'collapse'].includes(data.type)) {
          data.columns.forEach(col => {
            recursive(col.list, data)
          })
        } else if (data.type == 'batch') {
          //recursive(data.list)
          result.push(data)
        } else if (data.type == 'card' || data.type == 'block') {
          if (this.formDesignConfig?.indicatorCard) {
            result.push(data)
          }
          recursive(data.list, data)
        } else if (data.type == 'table') {
          data.trs.forEach(tr => {
            tr.tds.forEach(td => {
              recursive(td.list, data)
            })
          })
        } else {
          result.push(data)
        }
      })
    }
    recursive(list, parent ? parent :{})
    return result
  }

}